package com.ruoyi.system.utils;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    //应用私钥:
    public static String  privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC0ztMcmkiCN+MRgdcrpoNjdLMKTitbTZYJHy6Pjl2La9y3Ir63V89I+CytyWMFKXF1BR8OZE238s06diyuudAhEgiryiET92pwcmOEcAn3nj+6TJKNPsuivV9V9yEmhhUkSWj0rX5QtAdwzAzeQOHaunehxfiDjuNjjlkRTH5ZC+KY0o/zg+HGWStL8t/mm9/c8siDkdmHhk4xKSKnC8GbjxGiLSJb9PYCzS93lYDH14JQG62hP9rNqFUhkFlGyx+EZ+I6tJgyycgM46QuD+JDDyMriVWFwb4DhNunwvTYrg8tAfhgSShIsoLYsm2OA3e3eO/RfIalvEUtGKg/6CFtAgMBAAECggEARBKCRQiJOA2WfebmfdapLO6o4b5gcHWbgT+uJfoxmeY4KPL53sk+j3hwSdXCZBUgVDBjauW0pQbS4anQVNA8XdcOUK8/GOdTuZY3hYsxFXsM/zUJEAiOXmzx0cnDP1/H4euJimUvY28ERiFsztiar+D4XX3NfKp1Tewv+GVWRGujkVuY97m227fLVj3+KXOOyysM9fLeiMdj7aeynxu902w6qDoO+0/hdMtNTmV16IrMrd46LaXDaxluTjvfRfWKKHRrNhlUtwIInftVpbXyG2V7yaRCkm+/qbWAGKIk56T9typXkN+aNd4y0Xi3NdkKSsAmp++uDGDGQCQhwwWkuQKBgQD23BVNFKofOEv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aWqY4uYxbz15zQ7lfO/51C/14SrrEXK8/Mgd3orYm0FuHY3ElyfJXlppugniXCtXrcYu4TE62iUToYqyQb6gFIbz+d0LF0BkfAO/b4buItSDDqc+DANhr53MKpEwrdtq8ygfldjtSlAvo37QAm5/OdKc2VCJdjuWPrQ3Jy1Sfv1RGtUox9IPn34PBF2aVPY9WVZa7vNJyN2xeQIDAQAB";

    //生成支付订单接口
    public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000142633867");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        request.setReturnUrl("https://www.baidu.com/s?wd=%E6%94%AF%E4%BB%98%E5%AE%9D");
       request.setNotifyUrl("https://25eb-221-221-153-146.ngrok-free.app/pay/payNotify");
        model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney());
        model.setSubject(payData.getName());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        System.out.println(response.getBody());
        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000142633867");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
